<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<!DOCTYPE HTML>
<html>
<!--
Test Event rep
-->
<head>
  <meta charset="utf-8">
  <title>Rep test - Event</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test">
<script src="head.js" type="application/javascript;version=1.8"></script>
<script type="application/javascript;version=1.8">
window.onload = Task.async(function* () {
  let { Rep } = browserRequire("devtools/client/shared/components/reps/rep");
  let { Event } = browserRequire("devtools/client/shared/components/reps/event");

  try {
    // Test that correct rep is chosen
    const renderedRep = shallowRenderComponent(Rep, { object: getGripStub("testEvent") });
    is(renderedRep.type, Event.rep, `Rep correctly selects ${Event.rep.displayName}`);

    yield testEvent();
    yield testMouseEvent();
    yield testKeyboardEvent();
    yield testMessageEvent();
  } catch(e) {
    ok(false, "Got an error: " + DevToolsUtils.safeErrorString(e));
  } finally {
    SimpleTest.finish();
  }

  function testEvent() {
    const renderedComponent = renderComponent(Event.rep, { object: getGripStub("testEvent") });
    is(renderedComponent.textContent,
       "Event { isTrusted: true, eventPhase: 2, bubbles: false, 7 more… }",
       "Event rep has expected text content for an event");
  }

  function testMouseEvent() {
    const renderedComponent = renderComponent(Event.rep, { object: getGripStub("testMouseEvent") });
    is(renderedComponent.textContent,
       "MouseEvent { clientX: 62, clientY: 18, layerX: 0, 2 more… }",
       "Event rep has expected text content for a mouse event");
  }

  function testKeyboardEvent() {
    const renderedComponent = renderComponent(Event.rep, { object: getGripStub("testKeyboardEvent") });
    is(renderedComponent.textContent,
       "KeyboardEvent { key: \"Control\", charCode: 0, keyCode: 17 }",
       "Event rep has expected text content for a keyboard event");
  }

  function testMessageEvent() {
    const renderedComponent = renderComponent(Event.rep, { object: getGripStub("testMessageEvent") });
    is(renderedComponent.textContent,
       "MessageEvent { isTrusted: false, data: \"test data\", origin: \"null\", 7 more… }",
       "Event rep has expected text content for a message event");
  }

  function getGripStub(name) {
    switch (name) {
      case "testEvent":
        return {
          "type": "object",
          "class": "Event",
          "actor": "server1.conn23.obj35",
          "extensible": true,
          "frozen": false,
          "sealed": false,
          "ownPropertyLength": 1,
          "preview": {
            "kind": "DOMEvent",
            "type": "beforeprint",
            "properties": {
              "isTrusted": true,
              "currentTarget": {
                "type": "object",
                "class": "Window",
                "actor": "server1.conn23.obj37",
                "extensible": true,
                "frozen": false,
                "sealed": false,
                "ownPropertyLength": 760,
                "preview": {
                  "kind": "ObjectWithURL",
                  "url": "http://example.com"
                }
              },
              "eventPhase": 2,
              "bubbles": false,
              "cancelable": false,
              "defaultPrevented": false,
              "timeStamp": 1466780008434005,
              "originalTarget": {
                "type": "object",
                "class": "Window",
                "actor": "server1.conn23.obj38",
                "extensible": true,
                "frozen": false,
                "sealed": false,
                "ownPropertyLength": 760,
                "preview": {
                  "kind": "ObjectWithURL",
                  "url": "http://example.com"
                }
              },
              "explicitOriginalTarget": {
                "type": "object",
                "class": "Window",
                "actor": "server1.conn23.obj39",
                "extensible": true,
                "frozen": false,
                "sealed": false,
                "ownPropertyLength": 760,
                "preview": {
                  "kind": "ObjectWithURL",
                  "url": "http://example.com"
                }
              },
              "NONE": 0
            },
            "target": {
              "type": "object",
              "class": "Window",
              "actor": "server1.conn23.obj36",
              "extensible": true,
              "frozen": false,
              "sealed": false,
              "ownPropertyLength": 760,
              "preview": {
                "kind": "ObjectWithURL",
                "url": "http://example.com"
              }
            }
          }
        };

      case "testMouseEvent":
        return {
          "type": "object",
          "class": "MouseEvent",
          "actor": "server1.conn20.obj39",
          "extensible": true,
          "frozen": false,
          "sealed": false,
          "ownPropertyLength": 1,
          "preview": {
            "kind": "DOMEvent",
            "type": "click",
            "properties": {
              "buttons": 0,
              "clientX": 62,
              "clientY": 18,
              "layerX": 0,
              "layerY": 0
            },
            "target": {
              "type": "object",
              "class": "HTMLDivElement",
              "actor": "server1.conn20.obj40",
              "extensible": true,
              "frozen": false,
              "sealed": false,
              "ownPropertyLength": 0,
              "preview": {
                "kind": "DOMNode",
                "nodeType": 1,
                "nodeName": "div",
                "attributes": {
                  "id": "test"
                },
                "attributesLength": 1
              }
            }
          }
        };

      case "testKeyboardEvent":
        return {
          "type": "object",
          "class": "KeyboardEvent",
          "actor": "server1.conn21.obj49",
          "extensible": true,
          "frozen": false,
          "sealed": false,
          "ownPropertyLength": 1,
          "preview": {
            "kind": "DOMEvent",
            "type": "keyup",
            "properties": {
              "key": "Control",
              "charCode": 0,
              "keyCode": 17
            },
            "target": {
              "type": "object",
              "class": "HTMLBodyElement",
              "actor": "server1.conn21.obj50",
              "extensible": true,
              "frozen": false,
              "sealed": false,
              "ownPropertyLength": 0,
              "preview": {
                "kind": "DOMNode",
                "nodeType": 1,
                "nodeName": "body",
                "attributes": {},
                "attributesLength": 0
              }
            },
            "eventKind": "key",
            "modifiers": []
          }
        };

      case "testMessageEvent":
        return {
          "type": "object",
          "class": "MessageEvent",
          "actor": "server1.conn3.obj34",
          "extensible": true,
          "frozen": false,
          "sealed": false,
          "ownPropertyLength": 1,
          "preview": {
            "kind": "DOMEvent",
            "type": "message",
            "properties": {
              "isTrusted": false,
              "data": "test data",
              "origin": "null",
              "lastEventId": "",
              "source": {
                "type": "object",
                "class": "Window",
                "actor": "server1.conn3.obj36",
                "extensible": true,
                "frozen": false,
                "sealed": false,
                "ownPropertyLength": 760,
                "preview": {
                  "kind": "ObjectWithURL",
                  "url": ""
                }
              },
              "ports": {
                "type": "object",
                "class": "Array",
                "actor": "server1.conn3.obj37",
                "extensible": true,
                "frozen": false,
                "sealed": false,
                "ownPropertyLength": 0
              },
              "currentTarget": {
                "type": "object",
                "class": "Window",
                "actor": "server1.conn3.obj38",
                "extensible": true,
                "frozen": false,
                "sealed": false,
                "ownPropertyLength": 760,
                "preview": {
                  "kind": "ObjectWithURL",
                  "url": ""
                }
              },
              "eventPhase": 2,
              "bubbles": false,
              "cancelable": false
            },
            "target": {
              "type": "object",
              "class": "Window",
              "actor": "server1.conn3.obj35",
              "extensible": true,
              "frozen": false,
              "sealed": false,
              "ownPropertyLength": 760,
              "preview": {
                "kind": "ObjectWithURL",
                "url": ""
              }
            }
          }
        };

    }
  }
});
</script>
</pre>
</body>
</html>
